# 本文件是所有flask扩展的集合
import flask_login
from flask import url_for, request
from flask_mail import Mail
from flask_admin import Admin, AdminIndexView, expose
from flask_sqlalchemy import SQLAlchemy
from flask_security import Security, SQLAlchemyUserDatastore
from werkzeug.utils import redirect


class MyAdminView(AdminIndexView):
    def is_accessible(self):
        return flask_login.current_user.is_authenticated

    def inaccessible_callback(self, name, **kwargs):
        # redirect to login page if user doesn't have access
        return redirect(url_for('security.login', next=request.url))

    @expose()
    def index(self):
        return self.render(self._template, counter=100)


mail = Mail()

admin = Admin(name='得到 - 后台管理', template_mode='bootstrap3',
              index_view=MyAdminView(name='导航栏'))
db = SQLAlchemy()

from .models import User, Role

security = Security()
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
# babel = Babel()
